﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Web.Resources;

namespace Marbles.Core.Web.Controls
{
    [ToolboxData("<{0}:Toolbar runat=server></{0}:Toolbar>")]
    public class Toolbar : WebControl
    {
        [Category("Appearance"), TemplateInstance(TemplateInstance.Single), MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), Browsable(false)]
        public ITemplate ContentTemplate
        {
            get
            {
                return this._contentTemplate;
            }
            set
            {
                this._contentTemplate = value;
            }
        }

        public event CommandEventHandler ToolStripItemCommand;

        private ITemplate _contentTemplate;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this._contentTemplate != null)
            {
                Control container = new Control();
                this._contentTemplate.InstantiateIn(container);
                this.Controls.Add(container);

                for (int i = 0; i < container.Controls.Count; i++)
                {
                    if (container.Controls[i].GetType() == typeof(ToolStripIcon))
                    {
                        ToolStripIcon icn = (ToolStripIcon)container.Controls[i];
                        icn.CommandName = icn.ID;
                        icn.Command += new CommandEventHandler(OnToolStripIconCommand);
                    }
                }
            }
        }


        protected void OnToolStripIconCommand(object sender, CommandEventArgs e)
        {
            OnToolStripItemCommand(e);
            //if (Command != null) { Command(sender, e); }
        }

        protected virtual void OnToolStripItemCommand(CommandEventArgs e)
        {
            if (ToolStripItemCommand != null) { ToolStripItemCommand(this, e); }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            bool IsInToolStrip = (this.Parent.GetType() == typeof(ToolStrip));
            WebResources res = new WebResources();

            if (IsInToolStrip)
            {
                writer.AddStyleAttribute("width", this.Width.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
            }

            writer.AddAttribute("class", this.CssClass);
            writer.AddStyleAttribute("white-space", "nowrap");
            writer.AddStyleAttribute("width", this.Width.ToString());
            writer.AddStyleAttribute("overflow", "hidden");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute("border", "0");
            writer.AddAttribute("cellspacing", "0");
            writer.AddAttribute("cellpadding", "0");
            writer.AddAttribute("height", "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("align", "absmiddle");
            writer.AddStyleAttribute("width", "6px");
            writer.AddStyleAttribute("height", "14px");
            writer.AddAttribute("src", res.GrippyResourceUrl);
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            this.RenderChildren(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();

            if (IsInToolStrip) { writer.RenderEndTag(); }
        }

    }
}
